package com.ljfiction.book.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ljfiction.book.Baner.MyDanmuBean;
import com.ljfiction.book.MyView.CircularImageView;
import com.ljfiction.book.MyView.SuperButton;
import com.ljfiction.book.R;
import com.ljfiction.book.tools.ScreenUtils;
import com.ljfiction.book.tools.TimeTool;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Android on 2017/10/20.
 */

public class UserDuanpingAdapter extends BaseAdapter {
    private List<MyDanmuBean.DataBean.ListBean> listDuan = new ArrayList<>();
    private Context context;

    public UserDuanpingAdapter(List<MyDanmuBean.DataBean.ListBean> listDuan, Context context) {
        this.listDuan = listDuan;
        this.context = context;
    }

    @Override
    public int getCount() {
        if (listDuan.size() > 2) {
            return 2;
        } else {
            return listDuan.size();
        }
    }

    @Override
    public Object getItem(int position) {
        return listDuan.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.userduanpingitem, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.danmudenglimyUser.setText("LV" + String.valueOf(listDuan.get(position).getUserGrade()));
        viewHolder.contentMydanmuUser.setText(listDuan.get(position).getCommentContext());
        viewHolder.mydanmuMynameUser.setText(listDuan.get(position).getPseudonym());
        viewHolder.danmubooknameUser.setText("《"+listDuan.get(position).getBookName()+"》"+listDuan.get(position).getChapterName()+"  "+listDuan.get(position).getShowText().replaceAll("<LG>[a-zA-Z\\d-]{36}<LG>", "\n").replaceAll("\t", "").replaceAll("&nbsp;", " "));
        viewHolder.mudanmutimeUser.setText(TimeTool.getTimeAndChange(String.valueOf(listDuan.get(position).getCommentDateTime())));

        Glide.with(context).load(listDuan.get(position).getUserHeadPortraitURL()).override(ScreenUtils.getNum(context,52),ScreenUtils.getNum(context,52)).into(viewHolder.mydanmuMuimageUser);
        if (listDuan.get(position).getUserSex()==0){
            viewHolder.mydanmusexUser.setImageResource(R.mipmap.sex_n);
        }else {
            viewHolder.mydanmusexUser.setImageResource(R.mipmap.sex_v);
        }
        if (listDuan.get(position).getVipGrade()>0){
            viewHolder.isvipimageDanmuUser.setVisibility(View.VISIBLE);
        }else {
            viewHolder.isvipimageDanmuUser.setVisibility(View.GONE);
        }

        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.mydanmu_muimage_user)
        CircularImageView mydanmuMuimageUser;
        @BindView(R.id.isvipimage_danmu_user)
        ImageView isvipimageDanmuUser;
        @BindView(R.id.mydanmu_myname_user)
        TextView mydanmuMynameUser;
        @BindView(R.id.mydanmusex_user)
        ImageView mydanmusexUser;
        @BindView(R.id.danmudenglimy_user)
        SuperButton danmudenglimyUser;
        @BindView(R.id.mudanmutime_user)
        TextView mudanmutimeUser;
        @BindView(R.id.danmubookname_user)
        TextView danmubooknameUser;
        @BindView(R.id.content_mydanmu_user)
        TextView contentMydanmuUser;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
